<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\PriceOrdersModel;
use App\Model\SBillsModel;
use App\Model\WithdrawalModel;
use App\Services\SmsServices;
use Illuminate\Support\Facades\DB;

class CommonDao extends BaseDao
{
    /**
     * @throws SysExceptions
     */
    public static function sms_send($params = [])
    {
        checkForData(['phone'],['请输入手机号'],$params);
        SmsServices::send($params);
    }

    public static function pricesOrdersPage(array $all)
    {


        $query =  PriceOrdersModel::query();
        if(getShopId() != 0){
            $query->where('shop_id',getShopId());
        }

        return $query->orderByDesc('created_at')->where('status',1)->with('shopInfo')->with('userInfo')->paginate();
    }

    public static function confirmWiths(array $all)
    {
        $query = WithdrawalModel::query()->where('withdrawal_id',$all['withdrawal_id'])->where('status',0);
        if(!$query->exists()){
            self::error('数据不存在');
        }
        $info = $query->first()->toArray();
        DB::beginTransaction();
        try {
            $query->update([
                'status'=>$all['status']
            ]);


            if($info['user_id'] != 0){
                if($all['status'] == 2){
                    BillsDao::moneysUpdate($info['nums']+$info['fee'],$info['user_id'],'提现被拒绝资金返回!');
                }
            }

            if($info['shop_id'] != 0){
                if($all['status'] == 2){
                    SbillsDao::moneysUpdate($info['nums']+$info['fee'],$info['shop_id'],'提现被拒绝资金返回!');
                }
            }
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error($exception->getMessage());
        }

    }
}
